package com.golddaymicromall.activity;

import android.os.Bundle;
import android.util.Log;
import android.webkit.WebView;
import android.widget.ProgressBar;

import com.alibaba.fastjson.JSON;
import com.golddaymicromall.base.BaseActivity;
import com.golddaymicromall.util.RandomUtils;
import com.golddaymicromall.util.SPUtils;
import com.golddaymicromall.yibaozhifu.AES;
import com.golddaymicromall.yibaozhifu.EncryUtil;
import com.golddaymicromall.yibaozhifu.RSA;

import org.apache.http.conn.util.InetAddressUtils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.TreeMap;

public class YiBaoActivity extends BaseActivity {
    private String morderName;
    private int total_fee;
    private String Allprice;
    private String id;
    private String url;
    private String data = null;
    private String data1;
    private String encryptkey1;
    //测试编号17:33:23
    //10000418926
    private String callbackurl = "http://4g.mall.jt9988.com/asd/yibaozhifu";
    private String encryptkey = null;
    private WebView myibaowebview;
    //1商户RSA私钥：
    private String merchantPrivateKey = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAIT6LdwpOTa6PlTjoSStWY6b1A3HQUgXzXMcj6/UqSWZoBTxcBcGH8iLWuHYBK283+rFzzE2XaMpEGqNCf6wuTrihOW346NMjB6gfi3d1GU7RghulLjVeyFLi4SB6SjbGWIATQFlVLc73A6e4V0EIX24P9BcD7O4aVX2xbrzMjktAgMBAAECgYAQelPeeTk+v9tjd4WLXvO/CS5AutUrEtABmKNxfV+sahn9HmHuaHLdze7oF3WU9delZveiuILn6fm/ylRXvBxnscr9DkShWscpDyk2mRXK98axz/uqeS9VgL0dF36mpVzeieJtKmbHiYvJlweqisJvucpe9gB9MaBQB4OVzA+2HQJBAOS3nbwlGiBCXyQG336oehfZknFzie3hhdlNR01q96qS1IZF+4Oz1qFzCqwUJNGAA27Xh8uGwXS6zt0Fbg+hNysCQQCU1u6bY0GqXVxN1amnUNyuzGYkjNIS0w1mfz7LJoWLlHUd8wEQUOenc1nKukGL994VA0Jn4yjb5DDr7bcqMaUHAkAKDexYNgVU/7Xw/oBasAtV+xGN/TdGBtD8gYIt7BLhI71F/dOVsZUw2b7RW71WnFsKR11H3C+q8v9fBfsVos+rAkEAhhv/1P0jTslegTdy7TJ8FfhQVMCaDKIkYeVnhA7Kp9D1ki3CGJx9jrDuCC+5HdASSCausALLbw52Ui7YKmmfAwJBAM+lio2iwn8AbLCZpfFHZBcaJYc74z7cpCeCSFRieWBjpk6YW4aayGhJHe0SLYR4bVTfGEvLO/zD7qUqcTDODDw=";
    //2易宝RSA公钥：
    private String yeepayPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC+s2+rqyFRx6pRQ/gJNytvIM0B9kexSv6tJ+oFSkC2pFvxlUnD9ADqO2zZZJ+iYxg4xujLpdFCdvUjGybGhGzOO7bf7nP5zEL+oUjecR6Fspm94U8WeycFf6SvARnmao931oMXc8r+Y5pc3y/y5WW/KeSb97Qe63qwNC4Sg0Ub8QIDAQAB";
    ProgressBar progress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yi_bao);
        String ip = GetIp().toString();
        morderName = getIntent().getStringExtra("morderName");
        Log.e("订单编号", morderName);
        Allprice = getIntent().getStringExtra("mAllPrice");
        total_fee = (int) (Float.valueOf(Allprice) * 100);
        id = SPUtils.get(getApplicationContext(), "Id", "").toString();
        ionitList();
        initView();
    }

    private void ionitList() {
        TreeMap<String, Object> form = new TreeMap<String, Object>();
        form.put("merchantaccount", "2088421309270956");
        form.put("orderid", morderName);
        form.put("transtime", Integer.parseInt(String.valueOf(genTimeStamp())));
        form.put("amount", total_fee);
        form.put("productcatalog", "7");
        form.put("callbackurl", callbackurl);
        form.put("productname", "金天雪莲");
        form.put("identitytype", 2);
        form.put("identityid", id);
        form.put("terminaltype", 3);
        form.put("terminalid", "3");
        form.put("userip", GetIp().toString());
        String sign = EncryUtil.handleRSA(form, merchantPrivateKey);
        form.put("sign", sign);
        String mRandom = RandomUtils.getRandom(16);
        try {
            String jsonStr = JSON.toJSONString(form);
            data = AES.encryptToBase64(jsonStr, mRandom);
            data1 = toURLEncoded(data);
            encryptkey = RSA.encrypt(mRandom, yeepayPublicKey);
            encryptkey1 = toURLEncoded(encryptkey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        url = "https://ok.yeepay.com/paymobile/api/pay/request?data=" + data1 + "&encryptkey=" + encryptkey1 + "&merchantaccount=" + "2088421309270956";

    }

    private long genTimeStamp() {
        return System.currentTimeMillis() / 1000;
    }

    public static String toURLEncoded(String paramString) {
        if (paramString == null || paramString.equals("")) {
            return "";
        }
        try {
            String str = new String(paramString.getBytes(), "UTF-8");
            str = URLEncoder.encode(str, "UTF-8");
            return str;
        } catch (Exception localException) {
        }
        return "";
    }

    private void initView() {
        progress = (ProgressBar) findViewById(R.id.progress);
        myibaowebview = (WebView) findViewById(R.id.yibaowebview);
        myibaowebview.loadUrl(url);
    }

    public String GetIp() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> ipAddr = intf.getInetAddresses(); ipAddr
                        .hasMoreElements(); ) {
                    InetAddress inetAddress = ipAddr.nextElement();
                    // ipv4地址
                    if (!inetAddress.isLoopbackAddress()
                            && InetAddressUtils.isIPv4Address(inetAddress
                            .getHostAddress())) {

                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (Exception ex) {
        }
        return "";
    }
}
